using UnityEngine;
using QFramework;
using UnityEngine.Tilemaps;
using ProjectSurvivor;

namespace RepeaTileController
{
	public partial class Grid : ViewController
	{
		private Tilemap mUp;
		private Tilemap mDown;
		private Tilemap mLeft;
		private Tilemap mRight;
		private Tilemap mUpleft;
		private Tilemap mUpRight;
		private Tilemap mDownleft;
		private Tilemap mDownRight;
		private Tilemap mCenter;
		private int AreaX = 0;
		private int AreaY = 0;
		void CreateTileMaps()
		{
			mUp = Tilemap.InstantiateWithParent(transform);
			mDown = Tilemap.InstantiateWithParent(transform);
			mLeft = Tilemap.InstantiateWithParent(transform);
			mRight = Tilemap.InstantiateWithParent(transform);
			mUpleft = Tilemap.InstantiateWithParent(transform);
			mUpRight = Tilemap.InstantiateWithParent(transform);
			mDownleft = Tilemap.InstantiateWithParent(transform);
			mDownRight = Tilemap.InstantiateWithParent(transform);
			mCenter = Tilemap;
		}
		void UpdatePositions()
		{
			mUp.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
			mDown.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
			mLeft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
			mRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
			mUpleft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
			mUpRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
			mDownleft.Position(new Vector3((AreaX - 1) * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
			mDownRight.Position(new Vector3((AreaX + 1) * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
			mCenter.Position(new Vector3((AreaX + 0) * Tilemap.size.x, (AreaY + 0) * Tilemap.size.y));
		}
		void Start()
		{
			CreateTileMaps();
			UpdatePositions();
		}
        void Update()
        {
			if (Player.Default & Time.frameCount % 60 == 0)
			{ 
				var cellPos = Tilemap.layoutGrid.WorldToCell(Player.Default.transform.Position());
				AreaX = cellPos.x / Tilemap.size.x;
				AreaY = cellPos.y/Tilemap.size.y;
				UpdatePositions();
			}
			
        }
    }
}
